package top.shiyiri.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author Aunean
 * @date 2022/1/5
 */
/*
集合中常见方法的使用
 */
public class CollectionTest2 {

    @Test
    public void test1() {
        Collection<Object> list = new ArrayList<>();
        list.add(123);
        list.add(324);
        list.add(new String("Tom"));
        list.add(false);
        list.add(new Person("Jerry", 20));
        Person anna = new Person("Anna", 23);
        list.add(anna);

        //1.contains(Object obj)：判断当前集合中是否包含 obj
        System.out.println(list.contains(123));//true
        System.out.println(list.contains("Tom"));//true
        System.out.println(list.contains(new Person("Jerry", 20)));//true
        System.out.println(list.contains(new Person("Anna", 23)));//true

        //2.containsAll(Collection clo)：判断形参 clo 中的所有元素是否都包含在当前集合中
        List<Integer> list1 = Arrays.asList(123, 3241);
        System.out.println(list.containsAll(list1));//false
    }

    @Test
    public void test2() {
        //3.remove(Object obj)：从当前集合中移除 obj 元素
        Collection<Object> list = new ArrayList<>();
        list.add(123);
        list.add(324);
        list.add(new String("Tom"));
        list.add(false);
        list.add(new Person("Jerry", 20));
        Person anna = new Person("Anna", 23);
        list.add(anna);
        System.out.println(list);

        list.remove(123);
        System.out.println(list);

        list.remove(new Person("Jerry", 20));
        System.out.println(list);

        //4.removeAll(Collection col)：从当前集合中移除 col 中的所有元素
        Collection<Integer> col = Arrays.asList(123, 324);
        list.removeAll(col);
        System.out.println(list);
    }

    @Test
    public void test3() {
        Collection<Object> col = new ArrayList<>();
        col.add(123);
        col.add(456);
        col.add(new Person("Jerry", 20));
        col.add(new String("Tom"));
        col.add(false);

        //5.retainAll(Collection col)：交集，获取当前集合和col集合的交集，并返回给当前集合
        Collection<Object> col1 = Arrays.asList(123, 456, 789);
        System.out.println(col);//[123, 456, Person(name=Jerry, age=20), Tom, false]
        //col.retainAll(col1);
        System.out.println(col);//[123, 456]

        //6.equals(Object obj)：判断当前集合和形参集合的元素是否相同
        Collection<Object> col2 = new ArrayList<>();
        col2.add(123);
        col2.add(456);
        col2.add(new String("Tom"));
        col2.add(new Person("Jerry", 20));
        col2.add(false);
        //ArrayList有序，所以不相等
        System.out.println(col.equals(col2));//false
    }

    @Test
    public void test4() {
        Collection<Object> col2 = new ArrayList<>();
        col2.add(123);
        col2.add(456);
        col2.add(new String("Tom"));
        col2.add(new Person("Jerry", 20));
        col2.add(false);

        //7.hashCode()：返回当前对象的哈希值
        System.out.println(col2.hashCode());

        //8.集合 --> 数组：toArray()
        Object[] objects = col2.toArray();
        System.out.println(Arrays.toString(objects));

        //9.数组 --> 集合：Arrays.asList()
        List<Object> objects1 = Arrays.asList(objects);
        System.out.println(objects1);

        List<int[]> ints = Arrays.asList(new int[]{123, 456});
        System.out.println(ints);//[[I@50134894]

        List<Integer> list = Arrays.asList(new Integer[]{123, 456});
        System.out.println(list);//[123, 456]

        //10.iterator()：返回Iterator接口的实例，用于遍历集合元素。放在 IteratorTest.java中
    }
}
